import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import HomeLayout from '@/layout/home'
import MobileLayout from '@/layout/mobileLayout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    meta: { noCache: true },
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index'),
        meta: { noCache: true },
      },
    ],
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '/m-login',
    component: () => import('@/views/login/m-login.vue'),
    hidden: true,
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true,
  },
  {
    path: '/401',
    component: (resolve) => require(['@/views/401'], resolve),
    hidden: true,
  },
  {
    path: '/delink',
    component: () => import('@/views/link'),
    hidden: true,
  },

  {
    path: '/preview/:reportId',
    component: () => import('@/components/canvas/components/editor/PreviewEject'),
    hidden: true,
  },
  {
    path: '/previewScreenShot/:reportId/:backScreenShot',
    component: () => import('@/components/canvas/components/editor/PreviewEject'),
    hidden: true,
  },
  {
    path: '/previewFullScreen',
    component: () => import('@/components/canvas/components/editor/PreviewFullScreen'),
    hidden: true,
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'mct-shouye-zhihui' },
      },
    ],
  },
  {
    path: '/m-index',
    component: MobileLayout,
    redirect: '/m-index/m-dashboard',
    hidden: true,
    children: [
      {
        path: 'm-dashboard',
        name: 'MDashboard',
        component: () => import('@/views/dashboard/m-dashboard'),
        meta: { title: '首页', icon: 'el-icon-s-home' },
      },
      {
        path: 'm-link',
        name: 'MLink',
        component: () => import('@/views/link/m-link.vue'),
        meta: { title: '仪表盘', icon: 'el-icon-s-home', noCache: true },
      },
    ],
  },
  {
    path: '/home',
    component: HomeLayout,
    redirect: '/home/indicator-center',
    children: [
      {
        path: 'home-page',
        name: 'home-page',
        component: () => import('@/views/home'),
        meta: { title: '首页', noCache: true },
        hidden: true,
      },
      {
        path: 'indicator-center',
        name: 'indicator-center',
        component: () => import('@/views/indicator-center'),
        meta: { title: '指标中心', noCache: true },
        hidden: true,
      },
      {
        path: 'indicator-center/:id',
        name: 'IndicatorCenterView',
        component: () => import('@/views/indicator-center/view'),
        meta: { title: '指标详情', noCache: true },
        hidden: true,
      },
      {
        path: 'target-task',
        name: 'target-task',
        component: () => import('@/views/target-task'),
        meta: { title: '目标/任务', noCache: true },
        hidden: true,
        children: [
          {
            path: '/home/target-task/:id/aim-auth',
            name: 'AimAuth',
            component: () => import('@/views/aim/my-aim-auth'),
            meta: { title: '目标授权', noCache: true },
            hidden: true,
          },
          {
            path: '/home/target-task/task-schedule',
            name: 'HomeTaskSchedule',
            component: () => import('@/views/warning-task/task/schedule'),
            meta: { title: '任务进度上报', noCache: true },
            hidden: true,
          },
        ],
      },
      {
        path: 'data-analysis',
        name: 'data-analysis',
        component: () => import('@/views/data-analysis'),
        meta: { title: '数据分析', noCache: true },
        hidden: true,
      },
      {
        path: 'data-analysis/edit',
        name: 'HomeDataAnalysisEdit',
        component: () => import('@/views/panel/edit'),
        meta: { title: '数据分析编辑', noCache: true },
        hidden: true,
      },
      {
        path: 'index-catalog',
        name: 'index-catalog',
        component: () => import('@/views/index-catalog'),
        meta: { title: '指标目录', noCache: true },
        hidden: true,
      },
    ],
  },
  {
    path: '/costDashboard',  // 成本分析
    name: 'costDashboard',
    component: () => import('@/views/costDashboard/index.vue'),
    meta: { title: '成本刊本', noCache: true },
    hidden: true,
  }
]

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    // mode: 'hash',
    base: '/mct',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
